package com.lzgcoding.clock.timer

import com.lzgcoding.clock.base.BaseViewModel
import kotlinx.coroutines.flow.update

class TimerViewModel() : BaseViewModel<TimerState, TimerEvent, TimerAction>(
    initialState = run {
        TimerState(
            elapsedSeconds = 0,
            isRunning = false,
        )
    },
) {
    public override fun handleAction(action: TimerAction) {
        when (action) {
            TimerAction.UpdateElapsedSeconds -> {
                mutableStateFlow.update {
                    it.copy(
                        elapsedSeconds = it.elapsedSeconds + 1,
                    )
                }
            }
            TimerAction.Start -> {
                mutableStateFlow.update {
                    it.copy(
                        elapsedSeconds = 0,
                        isRunning = true,
                    )
                }

            }

            TimerAction.Cancel -> {
                mutableStateFlow.update {
                    it.copy(
                        elapsedSeconds = 0,
                        isRunning = false,
                    )
                }
            }
        }
    }
}

data class TimerState(
    val elapsedSeconds: Int = 0,
    val isRunning: Boolean = false,
)

sealed interface TimerEvent {

}

sealed interface TimerAction {
    data object UpdateElapsedSeconds : TimerAction
    data object Start : TimerAction
    data object Cancel : TimerAction
}